package com.mark.springboot.config;

import com.lowagie.text.DocumentException;
import com.mark.springboot.http.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;

/**
 * 全局异常处理器
 *
 * @author hongxy
 * @date 2021/8/23 21:04
 */

@ControllerAdvice
public class GlobalExceptionAdvice {
    private static Logger log = LoggerFactory.getLogger(GlobalExceptionAdvice.class);

    @ExceptionHandler(value = DocumentException.class)
    public R handleHttpException(HttpServletRequest req, DocumentException ex) {
        log.error("DocumentException: ", ex);
        return R.error();
    }

    @ExceptionHandler(value = FileNotFoundException.class)
    public R handleHttpException(HttpServletRequest req, FileNotFoundException ex) {
        log.error("FileNotFoundException: ", ex);
        return R.error();
    }

    @ExceptionHandler(value = Exception.class)
    public R handleHttpException(HttpServletRequest req, Exception ex) {
        log.error("Exception: ", ex);
        return R.error();
    }
}
